package com.ym.utils;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.ym.base.BaseEntity;

import java.nio.file.Paths;
import java.util.Collections;

public class Generator {

    public static void main(String[] args) {
        generatorCode("sys_user","generator");
    }

    public static void generatorCode(String tableName,String modelName) {
        //配置url，数据库，用户名，密码
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/test", "root", "123456")
                //全局配置
                .globalConfig(builder -> {
                    builder.disableOpenDir()
                            //作者可以不写
                            .author("义明")
                            //我这里写死generator，可以根据springboot模块名称更改，跟下面的包配置pathInfo同步（ps：同全局配置，两个是连着一起改的）
                            .outputDir(Paths.get(System.getProperty("user.dir"), modelName, "src", "main", "java").toString());
                })
                //包配置
                .packageConfig(builder -> {
                    builder.parent("com.ym")
                            .entity("entity")
                            .controller("controller")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            //同上全局配置，可以根据springboot模块名称更改（ps：同全局配置，两个是连着一起改的）
                            .pathInfo(Collections.singletonMap(OutputFile.xml, Paths.get(System.getProperty("user.dir"), modelName, "src", "main", "resources", "mapper").toString()));
                })
                //策略配置
                .strategyConfig(builder -> {
                    builder.addInclude(tableName)//表名
                            .entityBuilder()
                            .enableFileOverride()
                            .superClass(BaseEntity.class)//继承基类entity，提取共有字段
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .enableFileOverride()
                            .enableHyphenStyle()
                            .formatFileName("%sController")
                            .enableRestStyle()//@RestController
                            .serviceBuilder()
                            .enableFileOverride()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .mapperBuilder()
                            .enableFileOverride()
                            .enableBaseResultMap()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
                })
                // 自定义模板配置
                .injectionConfig(builder -> {
                    builder.beforeOutputFile((tableInfo, objectMap) -> {
                        // 获取实体类名
                        String entityName = tableInfo.getEntityName();
                        // 手动将首字母转为小写
                        String entityLowerFirst = entityName.substring(0, 1).toLowerCase() + entityName.substring(1);
                        // 将结果放入模板变量
                        objectMap.put("entityLowerFirst", entityLowerFirst);
                    });
                })
                // 指定自定义模板路径
                .templateConfig(builder -> {
                    builder.entity("/template/entity.java.vm")
                            .controller("/template/controller.java.vm")
                            .service("/template/service.java.vm")
                            .serviceImpl("/template/serviceImpl.java.vm")
                            .mapper("/template/mapper.java.vm");
                })
                .execute();
    }
}
